import json
from datetime import datetime

import pandas as pd

from util import get_industry_3, get_k_frame_industry, get_k_frame_custom, is_new_custom, \
    judge_industry_subsidies_customer, get_season, get_subside, get_back_id, get_k_frame_input, get_input_values

if __name__ == '__main__':
    industry = pd.read_csv("industry.csv")
    try:
        df = pd.read_excel('result.xlsx')
    except FileNotFoundError:
        df = pd.DataFrame(
            columns=['媒体', '亦芯-上海/亦芯-北京', '投放后台', '开户后台区域', '开户后台ID', '开户时间', '审核状态',
                     '账户ID', 'appid', '营业执照名称', '微信原始ID', '账户名称', '一级行业', '二级行业',
                     'KPI行业-媒体', '类目', '是否激励类目', '是否签link框', '所在省份', '2023年H1K框',
                     '2023年H2K框', '2024年K框', '集团简称', '集团sku', '销售', '一级事业部', '二级事业部',
                     '是否是全明芯', '业绩来源分类', '直客/渠道', '渠道/直客公司简称', '是否新开', '新开季度归属',
                     '运营类型', '流转记录', '流转状态', '是否享受S2', '享受S2周期', '是否享受S腰客',
                     '享受S腰客周期', '是否优客优链', '行一/行二/行三', '备注'])
    with open("list.json", "r") as f:
        item_list = json.load(f)['data']['list']
    rows_to_add = []
    for item in item_list:
        back = get_back_id(item['agency_uid'])
        back=get_back_id(back['parent_agency_uid'])
        frame_ind = get_k_frame_industry(agency_uid=item['agency_uid'], second_industry_id=item['industry_id'])
        frame = get_k_frame_custom(item['agency_uid'], item['uname'], item['industry_id'])
        res = judge_industry_subsidies_customer(item['agency_uid'], item['uname'], item['industry_id'])
        k_frame = get_k_frame_input(frame_ind, frame)
        print(f"您输入了{k_frame}")
        is_new = is_new_custom(item['agency_uid'], item['uname'])
        season = get_season() if is_new == "是" else ""
        subside = get_subside(res)
        clazz, is_active, is_link = ("其他", "否", "否") if k_frame == "K6" else ("", "", "")
        line123 = ""
        if item['industry_first_name'] == "零售和百货" and k_frame in ["K4", "K1", "K6"]:
            line123 = "行一"
        elif k_frame == "K2":
            line123 = "行二"
        elif k_frame in ["K3", "K4"]:
            line123 = "行三"
        sku, sale, first_cause_dept, second_cause_dept, source_class, zOrq, zOrq_simple, operate_type = get_input_values()
        rows_to_add.append({
            '开户后台区域': back['remark'].split("-")[1].replace("ADQ", ""),
            '开户后台ID': back['agency_uid'],
            '开户时间': datetime.strptime(item['reg_date'], '%Y-%m-%d %H:%M:%S').strftime('%Y/%m/%d'),
            "审核状态": item['customer_status_name'],
            "账户ID": item['uid'],
            "营业执照名称": item['uname'],
            "账户名称": item['uname'],
            "微信原始ID": item['uid'],
            "一级行业": item['industry_first_name'],
            "二级行业": item['industry_second_name'],
            "KPI行业-媒体": get_industry_3(industry, item),
            "所在省份": item['area_name'].split("-")[0],
            "2024年K框": k_frame,
            "是否新开": is_new,
            "新开季度归属": season,
            "类目": clazz,
            "是否激励类目": is_active,
            "是否签link框": is_link,
            "是否享受S2": "是" if subside else "否",
            "是否享受S腰客": "是" if subside else "否",
            "享受S2周期": "" if subside else f"{datetime.now().year}年全年不享受",
            "享受S腰客周期": "" if subside else f"{datetime.now().year}年全年不享受",
            "集团简称": item['uname'],
            "是否优客优链": "否" if k_frame == "K6" else "",
            "行一/行二/行三": line123,
            "集团sku": sku,
            "销售": sale,
            "一级事业部": first_cause_dept,
            "二级事业部": second_cause_dept,
            "业绩来源分类": source_class,
            "直客/渠道": zOrq,
            "渠道/直客公司简称": zOrq_simple,
            "运营类型": operate_type,
        })
    if len(rows_to_add) != 0:
        df = pd.concat([df, pd.DataFrame(rows_to_add)], ignore_index=True)
    # 为其他列赋值
    df['投放后台'] = 'ADQ'
    df['亦芯-上海/亦芯-北京'] = '亦芯-上海'
    df['媒体'] = '腾讯'
    df['类目'] = '其他'
    df['是否是全明芯'] = '否'
    df['备注'] = '苟小娟'
    df.to_excel('result.xlsx', index=False,)
